package com.junmo.www.RxJava;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.observables.ConnectableObservable;
import io.reactivex.schedulers.Schedulers;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

public class HotAndColdTest {
    @Test
    public  void test() {

    }

    /**
     * hot Observable
     * @throws InterruptedException
     */
    @Test
    public  void test06() throws InterruptedException {
        Consumer<Long> subscriber1 = aLong -> System.out.println("subscriber1 ：" +aLong);
        Consumer<Long> subscriber2 = aLong -> System.out.println("  subscriber2 ：" +aLong);
        Consumer<Long> subscriber3 = aLong -> System.out.println("      subscriber3 ：" +aLong);
        ConnectableObservable<Long> observable = Observable.interval(1,TimeUnit.SECONDS).publish();
        observable.connect();
        observable .subscribe(subscriber1) ;
        observable.subscribe(subscriber2) ;
        Thread.sleep(3000L) ;
        observable.subscribe(subscriber3);
        Thread.sleep(100000L) ;
    }

    /**
     * cold Observable
     * @throws InterruptedException
     */
    @Test
    public  void test05() throws InterruptedException {
        Consumer<Long> subscriber1 = aLong -> System.out.println("subscriber1 ：" +aLong);
        Consumer<Long> subscriber2 = aLong -> System.out.println("  subscriber2 ：" +aLong);
        Consumer<Long> subscriber3 = aLong -> System.out.println("      subscriber3 ：" +aLong);
        Observable<Long> observable = Observable.interval(1,TimeUnit.SECONDS);
        observable .subscribe(subscriber1) ;
        observable.subscribe(subscriber2) ;
        Thread.sleep(3000L) ;
        observable.subscribe(subscriber3);
        Thread.sleep(100000L) ;
    }
}
